

package com.maoshi.shop.user.platform.controller;


import cn.hutool.extra.emoji.EmojiUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maoshi.shop.bean.model.SysConfig;
import com.maoshi.shop.bean.param.ScoreConfigParam;
import com.maoshi.shop.common.annotation.SysLog;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.Json;
import com.maoshi.shop.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;


/**
 * 积分说明配置
 * @author lhd
 */
@RestController
@RequestMapping("/user/scoreExplainConfig")
public class ScoreExplainConfigController {
	@Autowired
	private SysConfigService sysConfigService;


	/**
	 * 配置信息
	 */
	@GetMapping("/info/{id}")
	@PreAuthorize("@pms.hasPermission('score:explainConfig:info')")
	public ResponseEntity<SysConfig> info(@PathVariable("id") String key){
		SysConfig config = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getParamKey,key));
		if(Objects.isNull(config)){
			return ResponseEntity.ok(new SysConfig());
		}
		return ResponseEntity.ok(config);
	}

	/**
	 * 保存配置
	 */
	@SysLog("保存配置")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('score:explainConfig:save')")
	public ResponseEntity<Void> save(@RequestBody  ScoreConfigParam scoreConfigParam){
		SysConfig config = new SysConfig();
		config.setParamKey(Constant.SCORE_EXPLAIN);
		config.setParamValue(scoreConfigParam.getParamValue());
		config.setRemark("积分说明");
		if (sysConfigService.count(new LambdaQueryWrapper<SysConfig>()
				.eq(SysConfig::getParamKey, config.getParamKey())) > 0) {
			throw new MaoshiShopBindException("系统已存在相同配置的key，无法重复添加,请刷新重试！");
		}
		sysConfigService.save(config);
		sysConfigService.removeSysConfig(Constant.SCORE_EXPLAIN);
		return ResponseEntity.ok().build();
	}

	/**
	 * 修改配置
	 */
	@SysLog("修改配置")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('score:explainConfig:update')")
	public ResponseEntity<Void> update(@RequestBody @Valid ScoreConfigParam scoreConfigParam){
		SysConfig config = new SysConfig();
		config.setId(scoreConfigParam.getId());
		config.setParamKey("SCORE_EXPLAIN");
		config.setParamValue(scoreConfigParam.getParamValue());
		config.setRemark("积分说明");
		scoreConfigParam.setId(null);
		SysConfig dbSysConfig = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>()
				.eq(SysConfig::getParamKey, config.getParamKey()));
		if (dbSysConfig != null && !Objects.equals(dbSysConfig.getParamKey(),config.getParamKey())) {
			throw new MaoshiShopBindException("系统已存在相同配置的key");
		}
		sysConfigService.updateById(config);
		sysConfigService.removeSysConfig(Constant.SCORE_CONFIG);
		return ResponseEntity.ok().build();
	}


}
